package com.zzyl.service.impl;

import com.google.common.collect.Lists;
import com.zzyl.constant.Constants;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WeChatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.StringUtils;
import com.zzyl.vo.LoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户管理
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MemberServiceImpl implements MemberService {

    private final WeChatService weChatService;

    private final MemberMapper memberMapper;

    private final JwtTokenManagerProperties jwtTokenManagerProperties;


    static ArrayList DEFAULT_NICKNAME_PREFIX = Lists.newArrayList(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );

    /**
     * 微信登录
     * @param dto
     * @return
     */
    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        //获取openid
        String openId = weChatService.getOpenId(dto.getCode());
        //根据openId查询用户,如果不存在则添加
        Member member = memberMapper.getByOpenid(openId);

        //获取手机号
        String phone = weChatService.getPhone(dto.getPhoneCode());

        String nickName = DEFAULT_NICKNAME_PREFIX.get((int) (Math.random() *
                DEFAULT_NICKNAME_PREFIX.size())) +
                StringUtils.substring(phone,4);

        if (member == null) {
            member = Member.builder()
                    .openId(openId)
                    .build();

            member.setName(nickName);
        }



        //新增或者保存
        saveOrUpdate(member,phone);

        //生成token返回
        Map<String, Object> map = new HashMap<>();
        map.put(Constants.JWT_USERID, member.getId());
        map.put(Constants.JWT_USERNAME, member.getName());

        String token = JwtUtil.createJWT(
                jwtTokenManagerProperties.getBase64EncodedSecretKey(),
                jwtTokenManagerProperties.getTtl(),
                map
        );

        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(member.getName());

        return loginVo;
    }

    private void saveOrUpdate(Member member, String phone) {

        if (ObjectUtil.notEqual(member.getPhone(), phone)) {
            member.setPhone(phone);
        }

        if (member.getId() != null) {
            memberMapper.update(member);
            return;
        }

        memberMapper.save(member);
    }



    /**
     * 根据id查询用户
     *
     * @param id 用户id
     * @return 用户信息
     */
    @Override
    public Member getById(Long id) {
        return memberMapper.selectById(id);
    }


}
